import {
    goIn,
    getDeptUserRules,
    getBoardProjectList
} from "@/api/api.js"
import { getDeptSkin } from "@/api/sys.js";
export const initCreate = {
    data() {
        return {
            navs: [],
            form: {
                username: "",
                password: ""
            },
            ruleList: [],
            btnLoading: false,
        }
    },
    methods: {
        //根据组织机构获取权限
        getUserRulesList(id, url) {
            debugger
            this.ruleList = [];
            let params = {
                deptId: id
            }
            getDeptUserRules(params).then(res => {
                if (res.code == 200) {
                    let arr = res.data.list;
                    let o = [];
                    arr.forEach(v => {
                        if (v.pid == 0) {
                            let obj = {
                                title: v.ruleName,
                                url: v.path,
                                id: v.ruleId,
                                pid: v.pid,
                                index: v.index,
                                code: v.code,
                                type: v.type,
                                children: [],
                                iconPath: v.iconPath == "" ? require("./../../assets/images/common/common.png") : require("./../../assets/images/" + v.code + "/" + v.iconPath + ".png"),
                            }
                            this.ruleList.push(obj)
                        }
                        if (v.type == 3) {
                            o.push(v.code);
                        }
                    })
                    //按钮权限的数组
                    window.localStorage.setItem("arr", JSON.stringify(o));
                    for (let i = 0; i < this.ruleList.length; i++) {
                        for (let j = 0; j < arr.length; j++) {
                            if (this.ruleList[i].id == arr[j].pid) {
                                let obj1 = {
                                    name: arr[j].ruleName,
                                    url: arr[j].path,
                                    id: arr[j].ruleId,
                                    pid: arr[j].pid,
                                    index: arr[j].index,
                                    code: arr[j].code,
                                    type: arr[j].type,
                                    icon: arr[j].iconPath || "",
                                    children: [],
                                };
                                this.ruleList[i].children.push(obj1)
                            }
                        }
                    }
                    for (let i = 0; i < this.ruleList.length; i++) {
                        for (let x = 0; x < this.ruleList[i].children.length; x++) {
                            for (let j = 0; j < arr.length; j++) {
                                if (this.ruleList[i].children[x].id == arr[j].pid && this.ruleList[i].children[x].type == 1) {
                                    let obj2 = {
                                        name: arr[j].ruleName,
                                        url: arr[j].path,
                                        id: arr[j].ruleId,
                                        pid: arr[j].pid,
                                        index: arr[j].index,
                                        code: arr[j].code,
                                        type: arr[j].type,
                                        icon: arr[j].iconPath || "",
                                        children: [],
                                    };
                                    this.ruleList[i].children[x].children.push(obj2);
                                }
                            }
                        }
                    }
                    if (localStorage.getItem("ruleCode") != "null") {
                        if (this.ruleList.length > 0) {
                            let list = [];
                            this.ruleList.forEach(i => {
                                if (i.code == (localStorage.getItem("ruleCode"))) {
                                    list = i.children;
                                }
                            })
                            this.$store.commit("setModalPage", list);
                            let code = localStorage.getItem("third_url").substring(1, localStorage.getItem("third_url").length);
                            for (let s = 0; s < list.length; s++) {
                                if (code == list[s].code) {
                                    localStorage.setItem("pathId", list[s].id);
                                } else {
                                    if (list[s].children && list[s].children.length > 0) {
                                        for (let k = 0; k < list[s].children.length; k++) {
                                            if (code == list[s].children[k].code) {
                                                localStorage.setItem("pathId", list[s].id);
                                                this.$store.commit("setModalChildPage", list[s].children);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (url) {
                        this.$router.push({
                            path: url
                        });
                    }
                } else {
                    this.$Message.warning(res.msg);
                }
            }).catch(error => {
                console.log(error)
            })
        },

        //用户登录
        async getLogin() {
            if (this.form.username === "" || this.form.password === "") {
                this.$Message.warning("用户名或密码不能为空");
                return;
            }
            this.btnLoading = true
            let params = {
                username: this.form.username,
                password: this.form.password
            }
            let result1 = await goIn(params).then(res => {
                if (res.code == 200) {
                    this.btnLoading = false
                    localStorage.setItem("username", res.data.nickname);
                    localStorage.setItem("userId", res.data.userId);
                    localStorage.setItem("loginMethod", res.data.loginMethod);
                    document.onkeydown = undefined;
                    return
                } else {
                    this.$Message.warning(res.msg);
                    this.btnLoading = false
                }
            })
            .catch(error => {
                this.btnLoading = false
                console.log(error);
            })

            let deptId = await getBoardProjectList().then((res) => {
                if (res.code == 200) {
                    return res.data.list[0].deptId
                    
                } else {
                    this.$Message.warning(res.msg);
                }
            })
            .catch((error) => {
                console.log(error);
            });

            let param = {
                deptId
            }
            let result2 = await getDeptSkin(param).then(res=>{
                let obj = res.data || {};
                let { titleLogo, name, ext1, style } = obj;
                localStorage.setItem("dzh_titleLogo", titleLogo || '');
                localStorage.setItem("dzh_name", name || '智慧工地大数据平台');
                localStorage.setItem("dzh_machProductName", ext1 || '机械云管家');
                localStorage.setItem("themeCode", style || 'DEFAULT');
                return
            })

            this.$router.push({
                path: "/lead"
            });
        },
    },
    mounted() {},
}
